package com.emapp.lccode.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.maps.CoordinateConverter;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.emapp.lccode.R;

import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * Author:YanshanZhang
 * E-mail:690810892@qq.com
 * Date: 2022/6/27 12:43
 * Desc:
 */
public class MapAppUtil {
    /**
     * 判断手机中是否安装指定包名的软件
     *
     * @param context
     * @param pkgName 包名
     */
    public static boolean isInstallApk(Context context, String pkgName) {
        List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packages.size(); i++) {
            PackageInfo packageInfo = packages.get(i);
            if (packageInfo.packageName.equals(pkgName)) {
                return true;
            } else {
                continue;
            }
        }
        return false;
    }

    /**
     * 跳转到百度地图
     *
     * @param context    com.baidu.BaiduMap
     * @param latitude   纬度
     * @param longtitude 经度
     * @param address    终点
     */
    public static void goBaiduMap(Context context, double latitude, double longtitude, String address) {
        if (isInstallApk(context, "com.baidu.BaiduMap")) {
            try {
                Intent intent = Intent.getIntent("intent://map/direction?destination=latlng:"
                        + latitude + ","
                        + longtitude + "|name:" + address + //终点：该地址会在导航页面的终点输入框显示
                        "&mode=driving&" + //选择导航方式 此处为驾驶
                        "region=" + //
                        "&src=#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                context.startActivity(intent);
            } catch (URISyntaxException e) {
                Log.e("goError", e.getMessage());
            }
        } else {
            Toast.makeText(context, "您尚未安装百度地图", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 跳转到高德地图
     *
     * @param context    com.autonavi.minimap
     * @param latitude   纬度
     * @param longtitude 经度
     * @param address    终点
     */
    public static void goGaodeMap(Context context, double latitude, double longtitude, String address) {
        if (isInstallApk(context, "com.autonavi.minimap")) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            Uri uri = Uri.parse("androidamap://navi?sourceApplication=appname&amp;poiname=" + address + "&lat=" + latitude
                    + "&lon=" + longtitude + "&dev=1" + "&style=2");
            intent.setData(uri);
            context.startActivity(intent);
//            try {
//                Intent intent = Intent.getIntent("androidamap://navi?sourceApplication=&poiname=" + address + "&lat=" + latitude
//                        + "&lon=" + longtitude + "&dev=0");
//                context.startActivity(intent);
//            } catch (URISyntaxException e) {
//                Log.e("goError", e.getMessage());
//            }
        } else {
            Toast.makeText(context, "您尚未安装高德地图", Toast.LENGTH_SHORT).show();
        }
    }

    private static SimpleDateFormat sdf = null;

    public static String formatUTC(long l, String strPattern) {
        if (TextUtils.isEmpty(strPattern)) {
            strPattern = "yyyy-MM-dd HH:mm:ss";
        }
        if (sdf == null) {
            try {
                sdf = new SimpleDateFormat(strPattern, Locale.CHINA);
            } catch (Throwable e) {
            }
        } else {
            sdf.applyPattern(strPattern);
        }
        return sdf == null ? "NULL" : sdf.format(l);
    }

    //高德坐标转换为GPS坐标
    public static double[] gcj02_To_Gps84(double lat, double lon) {

        double[] gps = new double[2];
        double ee = 0.00669342162296594323;
        double a = 6378245.0;
        double pi = 3.14159265358979324;
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        gps[0] = lat - dLat;
        gps[1] = lon - dLon;

        return gps;
    }


    private static double transformLat(double x, double y) {
        double pi = 3.14159265358979324;
        double ee = 0.00669342162296594323;
        double a = 6378245.0;
        double dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        dLat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        dLat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        dLat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return dLat;
    }

    private static double transformLon(double x, double y) {
        double pi = 3.14159265358979324;
        double ee = 0.00669342162296594323;
        double a = 6378245.0;
        double dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        dLon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        dLon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        dLon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return dLon;
    }

    /**
     * //其他转高德
     *
     * @param context
     * @param lat
     * @param lng
     * @return
     */
    public static LatLng other2Gao(Context context, double lat, double lng) {
        CoordinateConverter coordinateConverter = new CoordinateConverter(context);
        LatLng l = coordinateConverter.coord(new LatLng(lat, lng)).from(CoordinateConverter.CoordType.GPS).convert();
        return l;
    }
    public static LatLng other2Gao(Context context, LatLng latLng) {
        CoordinateConverter coordinateConverter = new CoordinateConverter(context);
        LatLng l = coordinateConverter.coord(latLng).from(CoordinateConverter.CoordType.GPS).convert();
        return l;
    }

    public static MyLocationStyle setupLocationStyle(int min) {
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);
        myLocationStyle.interval(min*1000);
//        // 自定义定位蓝点图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
                fromResource(R.drawable.gps_point));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(Color.argb(180, 3, 145, 255));
        //自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(0);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(Color.argb(10, 0, 0, 180));
        return myLocationStyle;
    }
}
